package com.core.controller;

import com.common.enums.ErrorCode;
import com.common.model.dto.user.UserRegisterDTO;
import com.common.response.BaseResponse;
import com.common.response.ResultUtils;
import com.core.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
public class RegisterController {

    @Resource
    private SysUserService sysUserService;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public BaseResponse userRegister(@RequestBody UserRegisterDTO userRegisterDTO) {
        if (StringUtils.isAnyBlank(userRegisterDTO.getUserName(), userRegisterDTO.getUserPassword())) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, "用户名或密码不能为空");
        }
        String userName = userRegisterDTO.getUserName().trim();
        String userPassword = userRegisterDTO.getUserPassword().trim();
        String confirmPassword = userRegisterDTO.getConfirmPassword().trim();
        long flag = sysUserService.userRegister(userName, userPassword, confirmPassword);
        if (flag <= 0) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR, "注册失败");
        }
        return ResultUtils.success(userName + "注册成功！欢迎新成员加入~~~");
    }
}
